package com.apes.scm.masterdata.finance.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.generator.Implementation;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * @desc 定义银行账户
 *
 * @author Bieb
 * @create 2018年8月9日15:16:31
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "mas_bank_account")
public class BankAccount extends Model {
    public static final String CONST_TABLE_NAME = "mas_bank_account";


    @Id
    @Comment("账号编码")
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "账号编码", padding = 4, implementation = Implementation.NO_GAP)
    private String id;

    @Name
    @Comment("账号名称")
    @Column(name = "name", nullable = false, length = 64)
    private String name;

    @Comment("银行")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bank_id", referencedColumnName = "id")
    private Bank bank;

    @Comment("银行分支机构")
    @Column(name = "bank_branch", length = 120)
    private String bankBranch;

    @Name
    @Comment("账户编码")
    @Column(name = "account_id", length = 120, nullable = false)
    private String accountId;

    @Name
    @Comment("账户名称")
    @Column(name = "account_name", length = 120)
    private String accountName;

    @Comment("收款户标记")
    @Column(name = "receipt_sign", nullable = false, columnDefinition = "number(1,0) default 0")
    private boolean receiptSign = false;

    @Comment("支出户标记")
    @Column(name = "pay_sign", nullable = false, columnDefinition = "number(1,0) default 0")
    private boolean paySign = false;

    @Comment("法人主体")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company;

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "party_id", referencedColumnName = "id")
    private Party partyId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type", referencedColumnName = "id")
    @Comment("收入类别")
    private Enumeration type;


    @Comment("有效标记")
    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 0")
    private boolean valid = true;

    @Comment("缴款有效")
    @Column(name = "prepare_sign", nullable = false, columnDefinition = "number(1,0) default 0")
    private boolean prepareSign = false;

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if (this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    @Transient
    private PartyRole _updater;

    @Transient
    public PartyRole get_updater() {
        if (this.getUpdater() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getUpdater());
    }
}
